<template>
  <div>
    <div>
      <el-card>
        <div slot="header" class="clearfix" style="text-align:center;font-size: 24px;color: #00b2ff">
          <span>配料报表</span>
        </div>
        <el-row :gutter="2">
          <el-col :span="12">
            <el-form class="search" :model="queryParams" :inline="true" v-show="showSearch"
                     @submit.native.prevent>
              <el-form-item label="搜索日期" prop="dt">
                <el-date-picker clearable size="small" style="width: 200px"
                                v-model="queryParams.dt"
                                type="date"
                                value-format="yyyy-MM-dd"
                                placeholder="选择日期"
                                @change="handleQuery">
                </el-date-picker>
              </el-form-item>
              <el-form-item>
                <!--                <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>-->
                <!--                <el-button type="primary" icon="el-icon-download" size="mini" @click="exportReport">导出</el-button>-->
              </el-form-item>
            </el-form>
          </el-col>
          <el-col :span="12">
            <el-form class="search" :inline="true" v-show="showSearch"
                     @submit.native.prevent>
              <el-form-item label="导出日期" prop="dt">
                <el-date-picker clearable
                                v-model="exportDateRange"
                                type="daterange"
                                value-format="yyyy-MM-dd HH:mm:ss"
                                range-separator="至"
                                start-placeholder="开始日期"
                                end-placeholder="结束日期"
                                size="small"
                                style="position: relative;width: 250px">
                </el-date-picker>
              </el-form-item>
              <el-form-item>
                <!--                <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>-->
                <el-button type="primary" icon="el-icon-download" size="mini" @click="exportReport">导出</el-button>
              </el-form-item>
            </el-form>
          </el-col>

        </el-row>

        <div v-loading="loading">
          <table class="table" style="width: 100%">
            <thead>
            <tr>
              <th colspan="25">配料生产统计</th>
            </tr>
            </thead>
            <tbody>
            <tr>
              <th>仓位</th>
              <th>1</th>
              <th>2</th>
              <th>3</th>
              <th>4</th>
              <th>5</th>
              <th>6</th>
              <th>7</th>
              <th>8</th>
              <th>9</th>
              <th>10</th>
              <th>11</th>
              <th>12</th>
              <th>13</th>
              <th>14</th>
              <th>15</th>
              <th>16</th>
              <th>17</th>
              <th>18</th>
              <th>19</th>
              <th>20</th>
              <th>21</th>
              <th>22</th>
              <th>23</th>
              <th rowspan="2">累计</th>
            </tr>
            <tr>
              <th>矿料</th>
              <td>{{ siloReportList[0].st2Ingredient1Name }}</td>
              <td>{{ siloReportList[0].st2Ingredient2Name }}</td>
              <td>{{ siloReportList[0].st2Ingredient3Name }}</td>
              <td>{{ siloReportList[0].st2Ingredient4Name }}</td>
              <td>{{ siloReportList[0].st2Ingredient5Name }}</td>
              <td>{{ siloReportList[0].st2Ingredient6Name }}</td>
              <td>{{ siloReportList[0].st2Ingredient7Name }}</td>
              <td>{{ siloReportList[0].st2Ingredient8Name }}</td>
              <td>{{ siloReportList[0].st2Ingredient9Name }}</td>
              <td>{{ siloReportList[0].st2Ingredient10Name }}</td>
              <td>{{ siloReportList[0].st2Ingredient11Name }}</td>
              <td>{{ siloReportList[0].st2Ingredient12Name }}</td>
              <td>{{ siloReportList[0].st2Ingredient13Name }}</td>
              <td>{{ siloReportList[0].st2Ingredient14Name }}</td>
              <td>{{ siloReportList[0].st2Ingredient15Name }}</td>
              <td>{{ siloReportList[0].st2Ingredient16Name }}</td>
              <td>{{ siloReportList[0].st2Ingredient17Name }}</td>
              <td>{{ siloReportList[0].st2Ingredient18Name }}</td>
              <td>{{ siloReportList[0].st2Ingredient19Name }}</td>
              <td>{{ siloReportList[0].st2Ingredient20Name }}</td>
              <td>{{ siloReportList[0].st2Ingredient21Name }}</td>
              <td>{{ siloReportList[0].st2Ingredient22Name }}</td>
              <td>{{ siloReportList[0].st2Ingredient23Name }}</td>
            </tr>
            <tr>
              <th style="color: #1c84f5">夜班统计</th>
              <td>{{ siloReportList[0].st2Night1Total }}</td>
              <td>{{ siloReportList[0].st2Night2Total }}</td>
              <td>{{ siloReportList[0].st2Night3Total }}</td>
              <td>{{ siloReportList[0].st2Night4Total }}</td>
              <td>{{ siloReportList[0].st2Night5Total }}</td>
              <td>{{ siloReportList[0].st2Night6Total }}</td>
              <td>{{ siloReportList[0].st2Night7Total }}</td>
              <td>{{ siloReportList[0].st2Night8Total }}</td>
              <td>{{ siloReportList[0].st2Night9Total }}</td>
              <td>{{ siloReportList[0].st2Night10Total }}</td>
              <td>{{ siloReportList[0].st2Night11Total }}</td>
              <td>{{ siloReportList[0].st2Night12Total }}</td>
              <td>{{ siloReportList[0].st2Night13Total }}</td>
              <td>{{ siloReportList[0].st2Night14Total }}</td>
              <td>{{ siloReportList[0].st2Night15Total }}</td>
              <td>{{ siloReportList[0].st2Night16Total }}</td>
              <td>{{ siloReportList[0].st2Night17Total }}</td>
              <td>{{ siloReportList[0].st2Night18Total }}</td>
              <td>{{ siloReportList[0].st2Night19Total }}</td>
              <td>{{ siloReportList[0].st2Night20Total }}</td>
              <td>{{ siloReportList[0].st2Night21Total }}</td>
              <td>{{ siloReportList[0].st2Night22Total }}</td>
              <td>{{ siloReportList[0].st2Night23Total }}</td>
              <td>{{ calculateTotal(siloReportList[0],"Night","Total") }}</td>
            </tr>
            <tr>
              <th style="color: #1c84f5">白班统计</th>
              <td>{{ siloReportList[0].st2Day1Total }}</td>
              <td>{{ siloReportList[0].st2Day2Total }}</td>
              <td>{{ siloReportList[0].st2Day3Total }}</td>
              <td>{{ siloReportList[0].st2Day4Total }}</td>
              <td>{{ siloReportList[0].st2Day5Total }}</td>
              <td>{{ siloReportList[0].st2Day6Total }}</td>
              <td>{{ siloReportList[0].st2Day7Total }}</td>
              <td>{{ siloReportList[0].st2Day8Total }}</td>
              <td>{{ siloReportList[0].st2Day9Total }}</td>
              <td>{{ siloReportList[0].st2Day10Total }}</td>
              <td>{{ siloReportList[0].st2Day11Total }}</td>
              <td>{{ siloReportList[0].st2Day12Total }}</td>
              <td>{{ siloReportList[0].st2Day13Total }}</td>
              <td>{{ siloReportList[0].st2Day14Total }}</td>
              <td>{{ siloReportList[0].st2Day15Total }}</td>
              <td>{{ siloReportList[0].st2Day16Total }}</td>
              <td>{{ siloReportList[0].st2Day17Total }}</td>
              <td>{{ siloReportList[0].st2Day18Total }}</td>
              <td>{{ siloReportList[0].st2Day19Total }}</td>
              <td>{{ siloReportList[0].st2Day20Total }}</td>
              <td>{{ siloReportList[0].st2Day21Total }}</td>
              <td>{{ siloReportList[0].st2Day22Total }}</td>
              <td>{{ siloReportList[0].st2Day23Total }}</td>
              <td>{{ calculateTotal(siloReportList[0],"Day","Total") }}</td>
            </tr>

            <tr>
              <th style="color: #1c84f5">中班统计</th>
              <td>{{ siloReportList[0].st2Mid1Total }}</td>
              <td>{{ siloReportList[0].st2Mid2Total }}</td>
              <td>{{ siloReportList[0].st2Mid3Total }}</td>
              <td>{{ siloReportList[0].st2Mid4Total }}</td>
              <td>{{ siloReportList[0].st2Mid5Total }}</td>
              <td>{{ siloReportList[0].st2Mid6Total }}</td>
              <td>{{ siloReportList[0].st2Mid7Total }}</td>
              <td>{{ siloReportList[0].st2Mid8Total }}</td>
              <td>{{ siloReportList[0].st2Mid9Total }}</td>
              <td>{{ siloReportList[0].st2Mid10Total }}</td>
              <td>{{ siloReportList[0].st2Mid11Total }}</td>
              <td>{{ siloReportList[0].st2Mid12Total }}</td>
              <td>{{ siloReportList[0].st2Mid13Total }}</td>
              <td>{{ siloReportList[0].st2Mid14Total }}</td>
              <td>{{ siloReportList[0].st2Mid15Total }}</td>
              <td>{{ siloReportList[0].st2Mid16Total }}</td>
              <td>{{ siloReportList[0].st2Mid17Total }}</td>
              <td>{{ siloReportList[0].st2Mid18Total }}</td>
              <td>{{ siloReportList[0].st2Mid19Total }}</td>
              <td>{{ siloReportList[0].st2Mid20Total }}</td>
              <td>{{ siloReportList[0].st2Mid21Total }}</td>
              <td>{{ siloReportList[0].st2Mid22Total }}</td>
              <td>{{ siloReportList[0].st2Mid23Total }}</td>
              <td>{{ calculateTotal(siloReportList[0],"Mid","Total") }}</td>
            </tr>

            <tr>
              <th rowspan="2" style="color: #1c84f5">日统计</th>
              <td>{{ siloReportList[0].st2Previous1Daily }}</td>
              <td>{{ siloReportList[0].st2Previous2Daily }}</td>
              <td>{{ siloReportList[0].st2Previous3Daily }}</td>
              <td>{{ siloReportList[0].st2Previous4Daily }}</td>
              <td>{{ siloReportList[0].st2Previous5Daily }}</td>
              <td>{{ siloReportList[0].st2Previous6Daily }}</td>
              <td>{{ siloReportList[0].st2Previous7Daily }}</td>
              <td>{{ siloReportList[0].st2Previous8Daily }}</td>
              <td>{{ siloReportList[0].st2Previous9Daily }}</td>
              <td>{{ siloReportList[0].st2Previous10Daily }}</td>
              <td>{{ siloReportList[0].st2Previous11Daily }}</td>
              <td>{{ siloReportList[0].st2Previous12Daily }}</td>
              <td>{{ siloReportList[0].st2Previous13Daily }}</td>
              <td>{{ siloReportList[0].st2Previous14Daily }}</td>
              <td>{{ siloReportList[0].st2Previous15Daily }}</td>
              <td>{{ siloReportList[0].st2Previous16Daily }}</td>
              <td>{{ siloReportList[0].st2Previous17Daily }}</td>
              <td>{{ siloReportList[0].st2Previous18Daily }}</td>
              <td>{{ siloReportList[0].st2Previous19Daily }}</td>
              <td>{{ siloReportList[0].st2Previous20Daily }}</td>
              <td>{{ siloReportList[0].st2Previous21Daily }}</td>
              <td>{{ siloReportList[0].st2Previous22Daily }}</td>
              <td>{{ siloReportList[0].st2Previous23Daily }}</td>
              <td>{{ calculateTotal(siloReportList[0],"Previous","Daily") }}</td>
            </tr>
            </tbody>
          </table>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script>
import {
  pageSiloReport,
  getSiloReport,
  delSiloReport,
  addSiloReport,
  updateSiloReport,
  exportSiloReport,
} from '@/api/app/siloReport/siloReport'

export default {
  name: 'SiloReport',
  components: {},
  data() {
    // 动态生成 1~23 的字段
    const genFields = (prefix, suffix, count = 23) =>
      Array.from({ length: count }, (_, i) => [`${prefix}${i + 1}${suffix}`, null]);
    return {
      // 遮罩层
      loading: true,
      preloading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 配料日报表格数据
      siloReportList: [{
        // 一次性展开所有字段
        ...[...genFields('st2Ingredient', 'Name'),
          ...genFields('st2Night', 'Total'),
          ...genFields('st2Day', 'Total'),
          ...genFields('st2Mid', 'Total'),
          ...genFields('st2Previous', 'Daily'),].reduce((acc, [k, v]) => {
          acc[k] = v;
          return acc;
        }, {})
      }],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dt: this.moment().subtract(1, 'day').format('YYYY-MM-DD'),
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      exportDateRange: [],
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询配料日报列表 */
    getList() {
      this.loading = true
      const genFields = (prefix, suffix, count = 23) =>
        Array.from({ length: count }, (_, i) => [`${prefix}${i + 1}${suffix}`, null]);

      pageSiloReport(this.queryParams).then(res => {
        if (res.data.total > 0) {
          this.siloReportList = res.data.rows
        } else {
          this.siloReportList = [{
            // 一次性展开所有字段
            ...[...genFields('st2Ingredient', 'Name'),
              ...genFields('st2Night', 'Total'),
              ...genFields('st2Day', 'Total'),
              ...genFields('st2Mid', 'Total'),
              ...genFields('st2Previous', 'Daily'),].reduce((acc, [k, v]) => {
              acc[k] = v;
              return acc;
            }, {})
          }]
        }
        this.total = res.data.total
        this.loading = false
      })
    },

    exportReport() {
      let query = {
        beginTime: this.exportDateRange[0],
        endTime: this.exportDateRange[1],
      }
      exportSiloReport(query).then(res => {
        this.download(res.msg)
      })
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },

    calculateTotal(report,teamName,endString) {
      const keys = Array.from({ length: 23 }, (_, i) => `st2${teamName}${i + 1}${endString}`);
      return keys.reduce((sum, key) => {
        const value = report[key] || 0; // 处理可能为 undefined 的值
        return sum + Number(value); // 确保值为数字
      }, 0);
    }
  }
}
</script>

<style lang="scss" scoped>

.title {
  font-size: 24px;
  margin-bottom: 5px;
  text-align: center;
}

::v-deep .el-table {
  .el-table__header-wrapper th, .el-table__fixed-header-wrapper th {
    height: 30px;
  }

  .el-table__cell {
    padding: 0;
  }

  .selected {
    background-color: #00b2ff !important;
    color: #ffffff !important;
  }

  .unproduced {
    background-color: #ffaa00;
    color: #ffffff;
  }

  .producing {
    background-color: #ff6f00;
    color: #ffffff;
  }

  .producing-converter {
    background-color: #13d70e;
    color: #ffffff;
  }

  .producing-lf {
    background-color: #029c00;
    color: #ffffff;
  }

  .producing-caster {
    background-color: #036800;
    color: #ffffff;
  }

  .produced {
    .alarm {
      color: #ff0000;
      animation: alarm-animation 1s infinite;
    }
  }

  @keyframes alarm-animation {
    0%, 50% {
      //background-color: #ff0000;
      //color: #FFFFFF;
      color: #606266;
    }
    51%, 100% {
      //background-color: #FFFFFF;
      color: #ff0000;
    }
  }

  .hover-row {
    /*td {
      background-color: #ffffff !important;
    }*/

    .cell {
      color: #000000;
    }
  }
}

::v-deep .el-tabs__header {
  margin-bottom: 0px;
}

.table {
  table-layout: fixed;
  border-collapse: collapse;
  width: 100%;
  text-align: center;

  th, td {
    border: 1px solid #cccccc;
    padding: 8px 0;
    font-size: 16px;
  }

  th {
    background-color: #9c9c9c;
    color: #ffffff;
  }

  .input {
    padding: 0;

    ::v-deep {
      .el-input__inner {
        height: 44px;
        line-height: 44px;
        background-color: #304056;
        color: #ffffff;
        font-size: 16px;
      }

      .el-textarea__inner {
        background-color: #304056;
        color: #ffffff;
      }

      ::placeholder {
        color: #dddddd;
      }
    }
  }
}

.ps {
  margin-top: 10px;
  font-size: 14px;

  span {
    margin-left: 10px;
    vertical-align: top;
  }

  .ps-icon {
    display: inline-block;
    width: 16px;
    height: 16px;
    vertical-align: top;
  }

  .unproduced {
    color: #ffaa00;

    .ps-icon {
      background-color: #ffaa00;
    }
  }

  .producing {
    color: #ff6f00;

    .ps-icon {
      background-color: #ff6f00;
    }
  }

  .producing-converter {
    color: #13d70e;

    .ps-icon {
      background-color: #13d70e;
    }
  }

  .producing-lf {
    color: #029c00;

    .ps-icon {
      background-color: #029c00;
    }
  }

  .producing-caster {
    color: #036800;

    .ps-icon {
      background-color: #036800;
    }
  }

  .produced {
    .ps-icon {
      background-color: #FFFFFF;
      border: 1px solid #000000;
    }
  }
}

.chart {
  height: 300px;
}

.chart2 {
  height: 400px;
}

.input-color {
  ::v-deep {
    .el-input__inner {
      background-color: #ffa200;
      color: #ffffff;
    }

    .el-textarea__inner {
      background-color: #ffa200;
      color: #ffffff;
    }

    ::placeholder {
      color: #dddddd;
    }
  }
}

.fill {
  ::v-deep .el-table__cell {
    padding: 0;

    .cell {
      padding: 0;
    }
  }
}
</style>

